//
//  MySelfInfoViewController.m
//  shuodi
//
//  Created by 钱坚 on 15/12/9.
//  Copyright © 2015年 Suzhou fish Information Technology Co., Ltd. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "MySelfInfoViewController.h"
#import "UINavigationBar+Awesome.h"
#import "MemberCenterViewController.h"
#import "ModifyPwdViewController.h"
#import "UIImageView+WebCache.h"
#import "ModifyPhoneViewController.h"
#import "modifyInfoViewController.h"

@interface MySelfInfoViewController (){
    InfoDataModel *personInfoModel;
    
    MemberInfoDataModel *input_data;
}
@end
@implementation MySelfInfoViewController

@synthesize getData,myScrollView,exit_button,filePath,myActionSheet,datePicker,datePickerView,datePickerSubView,cancel,confirm,headIconView,phoneLabel,nameLabel,birthdayLabel,userLabel;

- (instancetype)initWithInfo:(NSObject *)obj{
    self = [super init];
    if (self) {
        input_data = (MemberInfoDataModel*)obj;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[utils getBackGroupColorGray]];
    
    self.title = @"个人资料";
//    [self setCustomRightText:self action:@selector(saveClick:) text:NSLocalizedString(@"save", nil)];
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self initData];
    [self initView];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)initData{
    personInfoModel = [utils getPersonInfo];
    
    NSMutableArray *sectionArray = [[NSMutableArray alloc]initWithObjects:@{@"title":@"头像",@"value":personInfoModel.infomodel.m.headPic?personInfoModel.infomodel.m.headPic:@"touxiang_2x"}
                                    ,@{@"title":@"昵称",@"value":personInfoModel.infomodel.m.nick?personInfoModel.infomodel.m.nick:@""},
                                    @{@"title":@"用户名",@"value":personInfoModel.infomodel.m.nick?personInfoModel.infomodel.m.name:@""},
                                    @{@"title":@"手机号",@"value":personInfoModel.infomodel.m.phone?personInfoModel.infomodel.m.phone:@""},
                                    @{@"title":@"邮箱",@"value":personInfoModel.infomodel.m.email?personInfoModel.infomodel.m.email:@""},
                                    nil];
    NSMutableArray *sectionArray1 = [[NSMutableArray alloc]initWithObjects:@{@"title":@"密码管理",@"value":@"********"},
                                     nil];
    
    getData = [[NSMutableArray alloc]initWithObjects:sectionArray,sectionArray1, nil];
}

- (void)initView{
    [self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height)];
    
    [myScrollView setBackgroundColor:[UIColor clearColor]];
    myScrollView.showsHorizontalScrollIndicator = NO;
    myScrollView.delegate = self;
    myScrollView.showsVerticalScrollIndicator = FALSE;
    myScrollView.showsHorizontalScrollIndicator = FALSE;
    
    [self.view addSubview:myScrollView];
    
    CGFloat gendery = 0;
    
    CGFloat y = MyPropertyViewController_MARGIN;
    int tag = 1;
    for (int i = 0; i<getData.count; i++) {
        NSMutableArray *sectionData = [getData objectAtIndex:i];
        UIView *section = [[UIView alloc]initWithFrame:CGRectZero];
        section.backgroundColor = [UIColor whiteColor];
//        section.layer.masksToBounds=YES;
//        section.layer.cornerRadius = 4;
//        section.layer.borderWidth = 0.5;
//        section.layer.borderColor = [utils getBorderColorGray].CGColor;
        CGFloat cellH = 0;
        
        for (int j = 0 ; j < sectionData.count; j++) {
            NSDictionary *dic = [sectionData objectAtIndex:j];
            UIView *cellView = [self getCellView:cellH isLast:NO cellId:tag dic:dic];
            cellH += cellView.frame.size.height;
            [section addSubview:cellView];
            tag++;
        }
        section.frame = CGRectMake(MyPropertyViewController_MARGIN, y, self.view.frame.size.width - MyPropertyViewController_MARGIN*2, cellH);
        y = y+cellH+10;
        if (i == 0) {
            gendery = y;
        }
        [myScrollView addSubview:section];
        
    }
    
//    exit_button = [utils getButton:CGRectMake(OFFSET, y, self.view.frame.size.width/2, 45) withName:NSLocalizedString(@"exit_account", nil) ];
//    [exit_button sizeToFit];
//    [exit_button setFrame:CGRectMake( MyPropertyViewController_MARGIN, y+MyPropertyViewController_MARGIN, self.view.frame.size.width - MyPropertyViewController_MARGIN*2, 45)];
//    [self.view addSubview:exit_button];
//    [exit_button addTarget:self action:@selector(exitclick:) forControlEvents: UIControlEventTouchUpInside];
    
    y = y+MyPropertyViewController_MARGIN;
    [myScrollView addSubview:exit_button];
    [self initDatePicker];
    self.myScrollView.contentSize = CGSizeMake(1,y);
}


- (UIView *) getCellView :(CGFloat)y isLast:(BOOL)isLast cellId:(int)tag dic:(NSDictionary*)dic{
    NSString *title = [dic objectForKey:@"title"];
    NSString *value = [dic objectForKey:@"value"];
    
    UIView *cellView = [[UIView alloc]initWithFrame:CGRectMake(0, y, self.view.frame.size.width-MyPropertyViewController_MARGIN*2, MyPropertyViewController_CELL_HEIGHT)];
    cellView.backgroundColor = [UIColor whiteColor];
    
    UIButton *bgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    bgBtn.userInteractionEnabled = YES;
    bgBtn.backgroundColor = [UIColor whiteColor];
    bgBtn.frame = CGRectMake(0, 0, self.view.frame.size.width-MyPropertyViewController_MARGIN*2, MyPropertyViewController_CELL_HEIGHT);
    // 使用颜色创建UIImage
    CGSize imageSize=CGSizeMake(bgBtn.frame.size.width, bgBtn.frame.size.height);
    [bgBtn.layer setMasksToBounds:YES];
    UIImage *imageHighInit = [UIImage imageWithColor:[UIColor colorWithRed:223.0/255.0 green:223.0/255.0 blue:223.0/255.0 alpha:1.0] size:imageSize];
    [bgBtn setBackgroundImage:imageHighInit forState:UIControlStateHighlighted];
    [cellView addSubview:bgBtn];
    [bgBtn addTarget:self action:@selector(clickCell:) forControlEvents:UIControlEventTouchUpInside];
    bgBtn.tag = tag;
    
    CGFloat left = 10;
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    titleLabel.text = title;
    titleLabel.textColor = [utils getTextColorGray];
    titleLabel.font = [UIFont systemFontOfSize:13];
    [titleLabel sizeToFit];
    titleLabel.frame = CGRectMake(left, MyPropertyViewController_CELL_HEIGHT/2 - titleLabel.frame.size.height/2, titleLabel.frame.size.width, titleLabel.frame.size.height);
    [cellView addSubview:titleLabel];
    
//    if (value && ![value isEqualToString:@""]) {
        if(tag == 1){
            //            UIImage *image = [UIImage imageNamed:value];
            CGFloat imgSize = MyPropertyViewController_CELL_HEIGHT -5;
            headIconView = [[UIImageView alloc]init];
            headIconView.frame = CGRectMake(cellView.frame.size.width - left -imgSize -20, MyPropertyViewController_CELL_HEIGHT/2 - imgSize/2, imgSize, imgSize);
            headIconView.layer.cornerRadius = headIconView.frame.size.width / 2;
            headIconView.clipsToBounds = YES;
            headIconView.layer.masksToBounds = YES;
            [headIconView sd_setImageWithURL:[NSURL URLWithString:value] placeholderImage:[UIImage imageNamed:@"touxiang_2x"] options:SDWebImageRefreshCached|SDWebImageContinueInBackground|SDWebImageRetryFailed];
            [cellView addSubview:headIconView];
            
        }else{
            UILabel *valueLabel = [[UILabel alloc]initWithFrame:CGRectZero];
            valueLabel.text = value;
            valueLabel.font = [UIFont systemFontOfSize:13];
            valueLabel.textAlignment = NSTextAlignmentCenter;
            
            [cellView addSubview:valueLabel];
            if (tag == 2) {
                if (value && ![value isEqualToString:@""]) {
                    valueLabel.textColor = [utils getTextColorBlack];
                }else{
                    valueLabel.textColor = [utils getButtonColor];
                    valueLabel.text = @"未填写";
                }
                nameLabel = valueLabel;
            }else if (tag == 3) {
                if (value && ![value isEqualToString:@""]) {
                    valueLabel.textColor = [utils getTextColorBlack];
                }else{
                    valueLabel.textColor = [utils getButtonColor];
                    valueLabel.text = @"未填写";
                }
                userLabel = valueLabel;
            }else if(tag == 5){
                if (value && ![value isEqualToString:@""]) {
                    valueLabel.textColor = [utils getTextColorBlack];
                }else{
                    valueLabel.textColor = [utils getButtonColor];
                    valueLabel.text = @"未填写";
                }
                birthdayLabel = valueLabel;
            }else if(tag == 4){
                if (value && ![value isEqualToString:@""]) {
                    valueLabel.textColor = [utils getTextColorBlack];
                }else{
                    valueLabel.textColor = [utils getButtonColor];
                    valueLabel.text = @"立刻绑定";
                }
                phoneLabel = valueLabel;
            }else if(tag == 6){
                
                if ([personInfoModel.infomodel.m.pwdFlag isEqualToString:@"0"]) {
                    valueLabel.textColor = [utils getTextColorBlack];
                }else{
                    valueLabel.textColor = [utils getButtonColor];
                    valueLabel.text = @"未设置";
                }
            }
            
            [valueLabel sizeToFit];
            valueLabel.frame = CGRectMake(cellView.frame.size.width - left-valueLabel.frame.size.width -20, 0, valueLabel.frame.size.width, MyPropertyViewController_CELL_HEIGHT);
        }
        
//    }
//    else if(tag != 3){
        UIImageView *rightImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"jinru_2x"]];
        rightImg.frame = CGRectMake(cellView.frame.size.width - 5-rightImg.frame.size.width, MyPropertyViewController_CELL_HEIGHT/2-rightImg.frame.size.height/2, rightImg.frame.size.width, rightImg.frame.size.height);
        [cellView addSubview:rightImg];
//    }
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, MyPropertyViewController_CELL_HEIGHT-0.5, self.view.frame.size.width-MyPropertyViewController_MARGIN*2, 0.5)];
    line.backgroundColor = [utils getBorderColorGray];
    if (!isLast) {
        [cellView addSubview:line];
    }
    
    return cellView;
    
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    //呼出的菜单按钮点击后的响应
    if (buttonIndex == myActionSheet.cancelButtonIndex)
    {
        NSLog(@"取消");
    }
    
    switch (buttonIndex)
    {
        case 0:  //打开照相机拍照
            [self takePhoto];
            break;
            
        case 1:  //打开本地相册
            [self LocalPhoto];
            break;

    }
}

//开始拍照
-(void)takePhoto
{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        [self presentModalViewController:picker animated:YES];
    }else
    {
        NSLog(@"模拟其中无法打开照相机,请在真机中使用");
    }
}

//打开本地相册
-(void)LocalPhoto
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    //设置选择后的图片可被编辑
    picker.allowsEditing = YES;
    [self presentModalViewController:picker animated:YES];
}

//对图片尺寸进行压缩--
-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    
    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // End the context
    UIGraphicsEndImageContext();
    
    // Return the new image.
    return newImage;
}

//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{
    
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        //先把图片转成NSData
        UIImage* image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
        //        UIImage* image = [info objectForKey:@"UIImagePickerControllerCropRect"];
        
        //设置image的尺寸
        CGSize imagesize = image.size;
        imagesize = [utils resizeimage_small:imagesize];
        //对图片大小进行压缩--
        image = [self imageWithImage:image scaledToSize:imagesize];
        
        NSData *data;
        if (UIImagePNGRepresentation(image) == nil)
        {
            data = UIImageJPEGRepresentation(image, 1.0);
        }
        else
        {
            data = UIImagePNGRepresentation(image);
        }
        
        //图片保存的路径
        //这里将图片放在沙盒的documents文件夹中
        NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        
        //文件管理器
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
        [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
        
        NSString *imagename = [NSString stringWithFormat:@"/%@%@",[utils getCurrentTime],@".png"];
        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:imagename] contents:data attributes:nil];
        
        //得到选择后沙盒中图片的完整路径
        filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath, imagename];
        
        
        //关闭相册界面
        [picker dismissModalViewControllerAnimated:YES];
        
        //创建一个选择后图片的小图标放在下方
        //类似微薄选择图后的效果
        //        UIImageView *smallimage = [[UIImageView alloc] initWithFrame:
        //                                    CGRectMake(50, 120, 40, 40)];
        //
        //        smallimage.image = image;
        //        //加在视图中
        //        [self.view addSubview:smallimage];
        
        if (filePath && ![filePath isEqualToString:@""]) {
            NSArray *pics = [[NSArray alloc]initWithObjects:filePath, nil];
            [HttpNetWorkUtil requestuploadImg:@"1" filePaths:pics target:self success:^(id json) {
                
                if (json) {
                    int code = (int)[[json objectForKey:@"code"]integerValue];
                    if (code == 0) {
                        [headIconView setImage:image];
                        NSString *imgUrl =[json objectForKey:@"data"];
                        if (imgUrl && ![imgUrl isEqualToString:@""]) {
                            personInfoModel.infomodel.m.headPic = imgUrl;
                            NSData *person = [RFJModel toDataWithModel:personInfoModel];
                            [utils setObject:person forkey:PERSONINFO];
                        }
                        NSString *message =[json objectForKey:@"msg"];
                        [OMGToast showWithText:message duration:1.0];
                    }
                }
                
            } fail:^{}];
        }

    }
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    NSLog(@"您取消了选择图片");
    [picker dismissModalViewControllerAnimated:YES];
}


-(IBAction)clickCell:(id)sender{
    int tag = (int)((UIButton*)sender).tag;
    switch (tag) {
        case 1:{//头像
            //在这里呼出下方菜单按钮项
            myActionSheet = [[UIActionSheet alloc]
                             initWithTitle:nil
                             delegate:self
                             cancelButtonTitle:@"取消"
                             destructiveButtonTitle:nil
                             otherButtonTitles: @"拍照",@"选取现有的",nil];
            
            [myActionSheet showInView:self.view];
        }
            break;
        case 2:{//昵称
            NSString *str = nameLabel.text;
            if ([str isEqualToString:@"未填写"]) {
                str = @"";
            }
            modifyInfoViewController *view = [[modifyInfoViewController alloc] initWithInfo:@{@"title":str?str:@"",@"flag":@"1"}];
            [self.navigationController pushViewController:view animated:YES];
            
            /*
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"修改昵称"
                                                            message:nil
                                                           delegate:self
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"确定"
                                  , nil];
            alert.tag = 2;
            alert.alertViewStyle = UIAlertViewStylePlainTextInput;
            UITextField *input = [alert textFieldAtIndex:0];
            input.placeholder = @"请输入昵称";
            if (nameLabel.text && ![nameLabel.text isEqualToString:@""]) {
                input.placeholder = nameLabel.text;
            }
            
            [[UIView appearance] setTintColor:[utils getTextColorBlack]];
            
            [alert show];
             */
        }
            break;
        case 3:{//用户名
            
            NSString *str = userLabel.text;
            if ([str isEqualToString:@"未填写"]) {
                str = @"";
            }
            
            modifyInfoViewController *view = [[modifyInfoViewController alloc] initWithInfo:@{@"title":str?str:@"",@"flag":@"2"}];
            [self.navigationController pushViewController:view animated:YES];
            
            /*
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"修改用户名"
                                                            message:nil
                                                           delegate:self
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"确定"
                                  , nil];
            alert.tag = 3;
            alert.alertViewStyle = UIAlertViewStylePlainTextInput;
            UITextField *input = [alert textFieldAtIndex:0];
            input.placeholder = @"请输入用户名";
            if (userLabel.text && ![userLabel.text isEqualToString:@""]) {
                input.placeholder = userLabel.text;
            }
            
            [[UIView appearance] setTintColor:[utils getTextColorBlack]];
            
            [alert show];
             */
        }
            break;
        case 4:{//手机号
            ModifyPhoneViewController *view = [[ModifyPhoneViewController alloc] init];
            [self.navigationController pushViewController:view animated:YES];
        }
            break;
        case 5:{//邮箱
            NSString *str = birthdayLabel.text;
            if ([str isEqualToString:@"未填写"]) {
                str = @"";
            }
            
            modifyInfoViewController *view = [[modifyInfoViewController alloc] initWithInfo:@{@"title":str?str:@"",@"flag":@"3"}];
            [self.navigationController pushViewController:view animated:YES];
            
            /*
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"修改邮箱"
                                                            message:nil
                                                           delegate:self
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"确定"
                                  , nil];
            alert.tag = 5;
            alert.alertViewStyle = UIAlertViewStylePlainTextInput;
            UITextField *input = [alert textFieldAtIndex:0];
            input.placeholder = @"请输入邮箱";
            if (birthdayLabel.text && ![birthdayLabel.text isEqualToString:@""]) {
                input.placeholder = birthdayLabel.text;
            }
            [[UIView appearance] setTintColor:[utils getTextColorBlack]];
            
            [alert show];
             */
            
        }
            break;
            /*
        case 100:
            if (datePickerView.superview == nil) {
                
                //此处将Y坐标设在最底下，为了一会动画的展示
                datePickerSubView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 246);
                [self.view addSubview:datePickerView];
                
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.3f];
                [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
                [datePickerSubView setFrame:CGRectMake(0, self.view.frame.size.height-246, self.view.frame.size.width, 246)];
                [UIView commitAnimations];
            }
            break;
             */
        case 6:
        {
            ModifyPwdViewController *view = [[ModifyPwdViewController alloc]initWithInfo:personInfoModel.infomodel.m.pwdFlag];
//            ModifyPwdViewController *view = [[ModifyPwdViewController alloc]initWithInfo:@"0"];
            [self.navigationController pushViewController:view animated:YES];
        }
            break;
            
        default:
            break;
    }
}

-(IBAction)exitclick:(id)sender{
    [utils setObject:nil forkey:PERSONINFO];
    [utils setString:nil forkey:TOKEN];
    //    ShowViewController *view = [[ShowViewController alloc]init];
    //    [self.navigationController setViewControllers:@[view] animated:YES];
    [(AppDelegate*)[[UIApplication sharedApplication] delegate] event_gotologin:nil];
}

- (void)initDatePicker{
    datePickerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [datePickerView setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:237.0/255.0 alpha:0.5]];
    
    datePickerSubView = [[UIView alloc] initWithFrame:CGRectMake(0, datePickerView.frame.size.height, datePickerView.frame.size.width, 246)];
    [datePickerView addSubview:datePickerSubView];
    
    [datePickerSubView setBackgroundColor:[UIColor whiteColor]];
    
    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 30, datePickerView.frame.size.width, 216)];
    datePicker.datePickerMode = UIDatePickerModeDate;
    //    [datePicker addTarget:self action:@selector(chooseDate:) forControlEvents:UIControlEventValueChanged];
    
    [datePickerSubView addSubview:datePicker];
    
    cancel = [UIButton buttonWithType:UIButtonTypeCustom];
    cancel.backgroundColor = [UIColor clearColor];
    cancel.frame = CGRectMake(0, 0, 80, 30);
    [cancel setTitle:@"Cancel" forState:UIControlStateNormal];
    cancel.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    cancel.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    cancel.titleLabel.textAlignment = NSTextAlignmentCenter;
    cancel.titleLabel.font = [UIFont systemFontOfSize:16];
    [cancel setTitleColor:[utils getButtonColor] forState:UIControlStateNormal];
    [cancel setTitleColor:[utils getTextColorGray] forState:UIControlStateHighlighted];
    [cancel addTarget:self action:@selector(datePickerClick:) forControlEvents:UIControlEventTouchUpInside];
    [datePickerSubView addSubview:cancel];
    
    confirm = [UIButton buttonWithType:UIButtonTypeCustom];
    confirm.backgroundColor = [UIColor clearColor];
    confirm.frame = CGRectMake( datePickerView.frame.size.width-80, 0, 80, 30);
    [confirm setTitle:@"OK" forState:UIControlStateNormal];
    confirm.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    confirm.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    confirm.titleLabel.textAlignment = NSTextAlignmentCenter;
    confirm.titleLabel.font = [UIFont systemFontOfSize:16];
    [confirm setTitleColor:[utils getButtonColor] forState:UIControlStateNormal];
    [confirm setTitleColor:[utils getTextColorGray] forState:UIControlStateHighlighted];
    [confirm addTarget:self action:@selector(datePickerClick:) forControlEvents:UIControlEventTouchUpInside];
    [datePickerSubView addSubview:confirm];
}

-(IBAction)datePickerClick:(id)sender{
    UIButton *button = (UIButton*)sender;
    
    NSDate *selectedDate = datePicker.date;
    NSDate *currentDate = [NSDate date];
    
    if ([selectedDate compare:currentDate] == NSOrderedAscending) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy-MM-dd";
        NSString *dateString = [formatter stringFromDate:selectedDate];
        
        if (confirm == button) {
            birthdayLabel.text = dateString;
            CGRect frame = birthdayLabel.frame;
            [birthdayLabel sizeToFit];
            birthdayLabel.frame = CGRectMake(birthdayLabel.frame.origin.x+frame.size.width - birthdayLabel.frame.size.width, birthdayLabel.frame.origin.y, birthdayLabel.frame.size.width, birthdayLabel.frame.size.height);
        }
    }else{
        [OMGToast showWithText:NSLocalizedString(@"birthday_tip", nil) bottomOffset:100 duration:1.5];
    }
    
    if (datePickerView.superview) {
        [datePickerView removeFromSuperview];
    }
}


@end